package com.xuyiwei.mayi.controller;

import com.xuyiwei.mayi.base.BaseApiService;
import com.xuyiwei.mayi.base.ResponseBase;
import com.xuyiwei.mayi.constants.Constants;
import com.xuyiwei.mayi.entity.UserEntity;
import com.xuyiwei.mayi.feign.MemberServiceFegin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by xuyiwei on 2019/4/28.
 */
@Controller
public class RegisterController extends BaseApiService{
    private String REGISTER = "register";
    private String LOGIN = "login";

    @Autowired
    private MemberServiceFegin memberServiceFegin;

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String register(){
        return REGISTER;
    }

    /**
     * 加拉@requestbody只能接受json数据，如果是form表单提交则不需要
     * @param userEntity
     * @param request
     * @return
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String register(UserEntity userEntity, HttpServletRequest request){
        ResponseBase responseBase = memberServiceFegin.register(userEntity);
        if(!responseBase.getCode().equals(Constants.HTTP_RES_CODE_200)){
            request.setAttribute("error", responseBase.getMsg());
            return REGISTER;
        }
        return LOGIN;

    }
}
